<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Algo;

use App\Interf\Calculable;

class CancerTestingAlgo extends BaseTestingAlgo implements Calculable
{
    protected  $typeFourExpress;

    public function __construct($tid,$answerStr,$typeFourExpress=null)
    {
        parent::__construct($tid,$answerStr);

        $this->typeFourExpress=$typeFourExpress;
    }

    /**
     * 公共接口
     */
    public function calculate()
    {
     if($this->typeFourExpress){
            $fourTypeResult = $this->fourTypeArray($this->typeFourExpress,$this->answers);
            foreach($fourTypeResult as $v){
                array_push($this->answers,$v);
            }
        }
       return $this->entrance($this->tid,$this->answers);
    }


    /**将拼装后的答案转换成字符串
     * @return string
     */
    public function resultToStr(){
        return $this->arrToStr($this->answers);
    }

    /**获取R值
     * @param $tid
     * @param $answers
     * @return float|null
     */
    public function entrance($tid,$answers){
        $risk = null;
        $sex = $this -> getValuearray([1], $answers, 'weight');
        if(array_sum($sex) == 1){
            $risk = $this -> Manentrance($tid, $this->answers);
        }else{
            $risk = $this -> Womanentrance($tid, $this->answers);
        }
        return $risk;
    }

    /**男性最终R值
     * @param $tid
     * @param $answers
     * @return float|null
     */
    public function Manentrance($tid, $answers)
{
    $risk = null;
    switch($tid){
        case 4:
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = ($sum-10)/10.4;
            $risk = $ori_risk;
            $this -> postValue($this->answers,10,'weight',$risk);
            break;
        case 5:
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = $sum/20;
            $risk = $ori_risk;
            $this -> postValue($this->answers,10,'weight',$risk);
            break;
        case 6:
            $BMI = $this -> getValue( $this->answers,16,'weight');
            $BMIrisk = (float)$BMI > 27 ? 10 : 0;
            $this -> postValue($this->answers,17,'weight',$BMIrisk);
            $HeightRisk_man =$this->getValue( $this->answers,3,'answer') - 167.25;
            $this -> postValue($this->answers,18,'weight',$HeightRisk_man);
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = ($sum+$BMIrisk)/16.06;
            if($HeightRisk_man>15.24){
                $ori_risk = $ori_risk + (5/16.06);
            }
            $risk = $ori_risk;
            $this -> postValue($this->answers,20,'weight',$risk);
            break;
        case 7:
            $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
            if($smokingAnswer>=25){
                $smokingWeight=10;
            }elseif(25>$smokingAnswer && $smokingAnswer>= 15){
                $smokingWeight=5;
            }else{
                $smokingWeight=0;
            }
            $this -> postValue($this->answers,6,'weight',$smokingWeight);
            $BMI = $this -> getValue( $this->answers,10,'weight');
            $BMIrisk = (float)$BMI > 27 ? 10 : 0;
            $this -> postValue($this->answers,11,'weight',$BMIrisk);
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = $sum/11.45;
            $risk = $ori_risk;
            $this -> postValue($this->answers,12,'weight',$risk);
            break;
        case 8:
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = ($sum+10)/53.6;
            $risk = $ori_risk;
            $this -> postValue($this->answers,8,'weight',$risk);
            break;
        case 9:
            $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
            if($smokingAnswer>=25){
                $smokingWeight=10;
            }elseif(25>$smokingAnswer && $smokingAnswer>= 15){
                $smokingWeight=5;
            }else{
                $smokingWeight=0;
            }
            $this -> postValue($this->answers,6,'weight',$smokingWeight);
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = $sum/23.1;
            $risk = $ori_risk;
            $this -> postValue($this->answers,14,'weight',$risk);
            break;
        case 10:
            $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
            if($smokingAnswer>=25){
                $smokingWeight=25;
            }elseif(25 > $smokingAnswer && $smokingAnswer>= 15){
                $smokingWeight=10;
            }elseif(15 > $smokingAnswer && $smokingAnswer> 0){
                $smokingWeight=5;
            }else{
                $smokingWeight=0;
            }
            $this -> postValue($this->answers,6,'weight',$smokingWeight);
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = $sum/33.7;
            $risk = $ori_risk;
            $this -> postValue($this->answers,10,'weight',$risk);
            break;
        case 11:
            $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
            if($smokingAnswer>=25){
                $smokingWeight=50;
            }elseif(25 > $smokingAnswer && $smokingAnswer>= 15){
                $smokingWeight=10;
            }elseif(15 > $smokingAnswer && $smokingAnswer> 0){
                $smokingWeight=10;
            }else{
                $smokingWeight=0;
            }
            $this -> postValue($this->answers,6,'weight',$smokingWeight);
            $sum = $this->arr_sum($tid, $this->answers, 'weight');
            $ori_risk = $sum/51.6;
            $risk = $ori_risk;
            $this -> postValue($this->answers,12,'weight',$risk);
            break;
    }
    return $risk;
}

    /**女性最终R值
     * @param $tid
     * @param $answers
     * @return float|null
     *
     */
    public function Womanentrance($tid, $answers)
    {
        $risk = null;
        switch($tid){
            case 4:
                $risk = 0;
                $this -> postValue($this->answers,10,'weight',$risk);
                break;
            case 5:
                $sum = $this->arr_sum($tid, $answers, 'weight');
                $ori_risk = $sum/20;
                $risk = $ori_risk;
                $this -> postValue($this->answers,10,'weight',$risk);
                break;
            case 6:
                $BMI = $this -> getValue( $answers,16,'weight');
                $BMIrisk = (float)$BMI > 27 ? 10 : 0;
                $this -> postValue($this->answers,17,'weight',$BMIrisk);
                $HeightRisk_woman =$this->getValue( $answers,3,'answer') - 155.95;
                $this -> postValue($this->answers,19,'weight',$HeightRisk_woman);
                $sum = $this->arr_sum($tid, $answers, 'weight');
                $ori_risk = ($sum+$BMIrisk)/16.06;
                if($HeightRisk_woman>15.24){
                    $ori_risk = $ori_risk + (5/16.06);
                }
                $risk = $ori_risk;
                break;
            case 7:
                $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
                if($smokingAnswer>=25){
                    $smokingWeight=10;
                }elseif(25>$smokingAnswer && $smokingAnswer>= 15){
                    $smokingWeight=5;
                }else{
                    $smokingWeight=0;
                }
                $this -> postValue($this->answers,6,'weight',$smokingWeight);
                $BMI = $this -> getValue( $this->answers,10,'weight');
                $BMIrisk = (float)$BMI > 27 ? 10 : 0;
                $this -> postValue($this->answers,11,'weight',$BMIrisk);
                $sum = $this->arr_sum($tid, $this->answers, 'weight');
                $ori_risk = $sum/11.45;
                $risk = $ori_risk;
                $this -> postValue($this->answers,12,'weight',$risk);
                break;
            case 8:
                $sum = $this->arr_sum($tid, $this->answers, 'weight');
                $ori_risk = ($sum+10)/53.6;
                $risk = $ori_risk;
                $this -> postValue($this->answers,8,'weight',$risk);
                break;
            case 9:
                $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
                if($smokingAnswer>=25){
                    $smokingWeight=10;
                }elseif(25>$smokingAnswer && $smokingAnswer>= 15){
                    $smokingWeight=5;
                }else{
                    $smokingWeight=0;
                }
                $this -> postValue($this->answers,6,'weight',$smokingWeight);
                $sum = $this->arr_sum($tid, $this->answers, 'weight');
                $ori_risk = $sum/23.1;
                $risk = $ori_risk;
                $this -> postValue($this->answers,14,'weight',$risk);
                break;
            case 10:
                $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
                if($smokingAnswer>=25){
                    $smokingWeight=25;
                }elseif(25 > $smokingAnswer && $smokingAnswer>= 15){
                    $smokingWeight=10;
                }elseif(15 > $smokingAnswer && $smokingAnswer> 0){
                    $smokingWeight=5;
                }else{
                    $smokingWeight=0;
                }
                $this -> postValue($this->answers,6,'weight',$smokingWeight);
                $sum = $this->arr_sum($tid, $this->answers, 'weight');
                $ori_risk = $sum/33.7;
                $risk = $ori_risk;
                $this -> postValue($this->answers,10,'weight',$risk);
                break;
            case 11:
                $smokingAnswer = $this -> getValue( $this->answers,6,'answer');
                if($smokingAnswer>=25){
                    $smokingWeight=50;
                }elseif(25 > $smokingAnswer && $smokingAnswer>= 15){
                    $smokingWeight=10;
                }elseif(15 > $smokingAnswer && $smokingAnswer> 0){
                    $smokingWeight=10;
                }else{
                    $smokingWeight=0;
                }
                $this -> postValue($this->answers,6,'weight',$smokingWeight);
                $sum = $this->arr_sum($tid, $this->answers, 'weight');
                $ori_risk = $sum/51.6;
                $risk = $ori_risk;
                $this -> postValue($this->answers,12,'weight',$risk);
                break;
        }
        return $risk;
    }

    /**返回获取结论所需要的权重和分数
     * @param $tid
     * @param $answers
     * @param $value  weight或者answer
     * @return number
     */
    public function arr_sum($tid, $answers, $value)
    {
        $tid_arr = $this->choosecanner($tid);
        $weightarr = $this->getValuearray($tid_arr, $answers, $value);
        $sum = array_sum($weightarr);
        return $sum;
    }

    /**根据题号获取需要获取权重的题号数组
     * @param $tid
     * @return array
     */
    public function choosecanner($tid)
    {
        $arr = [];
        switch ($tid) {
            case 4://前列腺癌
                $arr = [4, 5, 6, 7, 8, 9];
                break;
            case 5://胃癌
                $arr = [5, 6, 7, 8, 9];
                break;
            case 6: //结肠癌
                $arr = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
                break;
            case 7://肾癌
                $arr = [5,6,7,8,9,11];
                break;
            case 8://黑色素瘤
                $arr = [5,6,7];
                break;
            case 9://胰腺癌
                $arr = [5,6,7,8,9,10,11,12,13];
                break;
            case 10://膀胱癌
                $arr = [5,6,7,8,9];
                break;
            case 11://肺癌
                $arr = [5,6,7,8,9,10,11];
                break;
        }
        return $arr;
    }


    /**Risk Level中额外参数值
     * @param $tid
     * @param $answers
     * @return null|number
     */
    public function extraprama($tid, $answers)
    {
        $extrasum = null;
        switch ($tid) {
            case 6:
                $arr = [16];
                $extrasum = $this -> getValuearray($arr, $answers, 'weight');
                if(array_sum($extrasum)>27){
                    $extrasum=10;
                }else{
                    $extrasum=0;
                }
                break;
        }
        return $extrasum;
    }
    /**数组赋值
     * @param $answers
     * @param $index
     * @param $valueType
     * @param $value
     * @return mixed 二维数组
     */
    public function postValue($answers,$index,$valueType,$value){
        foreach($answers as $k =>$v){
            if($v['index'] == $index){
                $answers[$k][$valueType]=$value;
            }
        }
        return $this->answers=$answers;
    }


}